<?php
namespace app\admin\controller;

use app\admin\model\AuthGroup;
use app\BaseController;
use think\facade\View;
use think\facade\Request;

class Auth extends BaseController
{
    //权限节点列表
    public function menu()
    {
        //查询列表
        $list = \app\admin\model\Auth::authMenu();
        View::assign('list',$list);
        return  View::fetch();
    }
    //添加权限
    public function menuAdd()
    {
        return View::fetch();
    }
    //权限组列表
    public function auth()
    {
        $list = AuthGroup::authGroup();
        View::assign('list',$list);
        return View::fetch();
    }
    //权限组添加
    public function authAdd()
    {
        $list = \app\admin\model\Auth::authList();
        View::assign('list',$list);
        if (Request::isPost()){
            $post = Request::post();
            $auths = implode(',',$post['auths']);
            $add = [
                'title' =>$post['aname'],
                'rules' =>$auths
            ];
            list($res,$msg) = AuthGroup::authGroupAdd($add);
            if ($res){
                $this->success($msg,url('Auth/auth'));
            }else{
                $this->error($msg);
            }
        }
        return View::fetch();
    }
    //权限组修改
    public function authEdit()
    {

        if (Request::isPost()){
            $post = Request::post();
            $auths = implode(',',$post['auths']);
            $edit = [
                'id' => $post['id'],
                'title' =>$post['aname'],
                'rules' =>$auths
            ];
            list($res,$msg) = AuthGroup::authGroupEdit($edit);
            if ($res){
                $this->success($msg,url('Auth/auth'));
            }else{
                $this->error($msg);
            }
        }else{
            $list = \app\admin\model\Auth::authList();
            View::assign('list',$list);
            $get = Request::get();
            $line = AuthGroup::where('id','=',$get['id'])->find();
            $line['rules'] = explode(',',$line['rules']);
            View::assign('line',$line);
        }
        return View::fetch();
    }
    //权限组删除
    public function authDel()
    {
        $get = Request::get();
        list($res,$msg) = AuthGroup::authGroupDel($get);
        if ($res){
            $this->success($msg,url('Auth/auth'));
        }else{
            $this->error($msg);
        }
    }

}